//
//  IGLightVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/11/17.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGLightVC.h"
#import "IGLightApi.h"
@interface IGLightVC ()<UITableViewDelegate, UITableViewDataSource>
{
    UISwitch *switchV;
    UISlider *sliderV;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) IGEquRemoteModel *model;
@property (nonatomic, strong) IGLightApi *api;

@end

@implementation IGLightVC

- (instancetype)initWithModel:(IGEquRemoteModel *)model
{
    self = [super init];
    if (self)
    {
        self.model = model;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor mainBackgroundColor];
    self.api = [IGLightApi new];
//    self.title = @"亮度调节";
    self.title = NSLocalizedString(@"Brightness", nil);
    [self.view addSubview:self.tableView];
//    [self ig_setupRightButtonItemWithTitle:@"确定" image:nil highLightedImage:nil];
    [self ig_setupRightButtonItemWithTitle:NSLocalizedString(@"Confirm", nil) image:nil highLightedImage:nil];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorInset = UIEdgeInsetsMake(0, -100, 0, 0);
    }
    return _tableView;
}

- (void)ig_rightButtonPressed:(id)sender
{
    _api.lightOn = switchV.isOn ? @"0" : @"1";
    _api.light = [NSString stringWithFormat:@"%ld",(NSInteger)(sliderV.value * 10)];
    _api.macId = _model.macId;
    [_api startWithDecodeClass:[IGLightResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
//            [self presentMessageTips:@"修改成功"];
            [self presentMessageTips:NSLocalizedString(@"Change Successfully", nil)];
            self.model.lightOn = _api.lightOn;
            self.model.light = _api.light;
        }
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"IGScreenModelVCID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    cell.textLabel.font = [UIFont font16];
    cell.textLabel.textColor = [UIColor c4Color];

    if (indexPath.section == 0)
    {
        UIImageView *smallV = [[UIImageView alloc] init];
        [cell addSubview:smallV];
        [smallV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(13);
            make.left.mas_equalTo(15);
            make.width.height.mas_equalTo(24);
        }];
        smallV.image = [UIImage imageNamed:@"light_off"];
        
        UIImageView *bigV = [[UIImageView alloc] init];
        [cell addSubview:bigV];
        [bigV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(9);
            make.right.mas_equalTo(-15);
            make.width.height.mas_equalTo(32);
        }];
        bigV.image = [UIImage imageNamed:@"light_on"];
        
        sliderV = [[UISlider alloc] init];
        [cell addSubview:sliderV];
        [sliderV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(49);
            make.right.mas_equalTo(-57);
            make.center.equalTo(cell);
        }];
        sliderV.value = [self.model.light floatValue] / 10.0;
    }
    else if (indexPath.section == 1)
    {
//        cell.textLabel.text = @"自动调节亮度";
        cell.textLabel.text = NSLocalizedString(@"Auto Brightness Adjust", nil);
        switchV = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        switchV.on = ![self.model.lightOn boolValue];
        cell.accessoryView = switchV;
    }
    return cell;
}




@end
